package com.example.administrator.about_huanxin.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.about_huanxin.R;
import com.example.administrator.about_huanxin.ShareprefenceUtils;
import com.example.administrator.about_huanxin.model.UserEntity;
import com.example.administrator.about_huanxin.presenter.activity.LoginPresenter;
import com.example.administrator.about_huanxin.presenter.activity.LoginPresenterImp;
import com.example.administrator.about_huanxin.view.activity.LoginActivityView;

public class LoginActivity extends BaseActivity implements LoginActivityView {

    private EditText ed_name, ed_pwd;
    private Button btn_login;
    private LoginPresenter loginPresenter;

    @Override
    public void setTitleText(TextView tv) {
        tv.setText("登录");
    }

    @Override
    public void setVisibleBtn(Button btn) {
        btn.setVisibility(View.GONE);
    }

    @Override
    public View addContent(LayoutInflater inflater, FrameLayout content) {
        View view = inflater.inflate(R.layout.activity_login,content,true);
        return view;
    }

    @Override
    public void initContent(View view) {
        initView(view);
    }

    private void initView(View view) {
        ed_name = (EditText) view.findViewById(R.id.ed_name);
        ed_pwd = (EditText) view.findViewById(R.id.ed_pwd);
        btn_login = (Button) view.findViewById(R.id.login_btn);

        loginPresenter = new LoginPresenterImp(this);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(ed_name.getText().toString()) && !TextUtils.isEmpty(ed_pwd.getText().toString())) {
                    login(ed_name.getText().toString(), ed_pwd.getText().toString());
                }else{
                    Toast.makeText(LoginActivity.this,"请填写完整信息",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void login(String name, String pwd) {
        loginPresenter.login(name, pwd);
    }

    @Override
    public void afterLogin(boolean bool) {
        if (bool) {
            Log.e("Logdin...","登录成功");
        } else {
            Log.e("Logdin...","登录失败");
        }
    }

    //从Bmob获取数据的回调
    @Override
    public void getUserInfo(UserEntity userEntity) {
        if(userEntity != null){
            Log.e("成功啦",userEntity.getNickname()+"..."+userEntity.getUsername()+"..."+userEntity.getPassword()+"..."+userEntity.getIcon().getUrl());
            ShareprefenceUtils.setResult(LoginActivity.this, "user", userEntity.getUsername());
            ShareprefenceUtils.setResult(LoginActivity.this, "pwd", userEntity.getPassword());
            ShareprefenceUtils.setResult(LoginActivity.this, "nick", userEntity.getNickname());
            ShareprefenceUtils.setResult(LoginActivity.this, "url", userEntity.getIcon().getUrl());
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }
    }
}
